package cn.itcast.solr;

import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * SolrCreateTest
 * @author xyj
 * @data 2018/8/21
 */
public class SolrCreateTest {

    @Test
    public void createTest() throws IOException, SolrServerException {
        //连接solr服务器
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建文档对象
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", 1);
        document.addField("title", "小米手机,为发烧而生");

        //写入solr文档
        server.add(document);
        //提交
        server.commit();
    }


    @Test
    public void test2() throws IOException, SolrServerException {
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
        Item item = new Item();
        item.setId("5");
        item.setTitle("三星手机,炸弹专家");
        server.addBean(item);
        server.commit();


    }

    @Test
    public void deleteTest() throws IOException, SolrServerException {
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");

        server.deleteByQuery("title:三星");

        server.commit();
    }

    @Test
    public void testQuery() throws SolrServerException {
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");

        SolrQuery query = new SolrQuery("*:*");

        QueryResponse response = server.query(query);

        long found = response.getResults().getNumFound();
        System.out.println("一共发现:" + found + "条数据");

        SolrDocumentList documents = response.getResults();
        for (SolrDocument solrDocument : documents) {
            System.out.println("id:" + solrDocument.getFieldValue("id"));
            System.out.println("title:" + solrDocument.getFieldValue("title"));
        }
    }

    @Test
    public void testQuery2() throws SolrServerException {
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");

        SolrQuery query = new SolrQuery("*:*");
        query.setSort("id", SolrQuery.ORDER.desc);

        QueryResponse response = server.query(query);

        List<Item> beans = response.getBeans(Item.class);

        for (Item item : beans) {
            System.out.println(item);
        }
    }


    @Test
    public void testPageTest() throws SolrServerException {
        int curPage = 2;
        int pageSize = 2;
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
        SolrQuery query = new SolrQuery("title:*");
        query.setSort("id", SolrQuery.ORDER.asc);
        query.setStart((curPage - 1) * pageSize);
        query.setRows(pageSize);

        QueryResponse response = server.query(query);
        List<Item> items = response.getBeans(Item.class);
        for (Item item : items) {
            System.out.println(item);
        }
    }

    @Test
    public void testHighLight() throws SolrServerException {

        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");

        SolrQuery query = new SolrQuery("title:手机");

        query.setHighlightSimplePre("<em>");
        query.setHighlightSimplePost("</em>");
        query.addHighlightField("title");

        QueryResponse response = server.query(query);
        List<Item> items = response.getBeans(Item.class);

        Map<String, Map<String, List<String>>> map = response.getHighlighting();
        items.forEach(item -> {System.out.println("id:" + item.getId());
            System.out.println("title:" + map.get(item.getId()).get("title").get(0));});

       /* for (Item item : items) {
            System.out.println("id:" + item.getId());
            System.out.println("title:" + map.get(item.getId()).get("title").get(0));

        }*/
    }

    @Test
    public void test1(){
        // 准备一个集合
        List<Integer> list = Arrays.asList(10, 5, 25, -15, 20);
        list.sort((i1,i2) ->  i1-i2);
        list.forEach(a -> System.out.println(a));
    }


    @Test
    public void test4(){
        List<Integer> list = Arrays.asList(1000,2000,3000);


        Integer num = 2000;
        List<Integer> compareList = CollectionUtils.convert(list, num::compareTo);
        System.out.println(compareList);

    }

    @Test
    public void test111() throws IOException, SolrServerException {
        CloudSolrServer server = new CloudSolrServer("192.168.56.101:2181,192.168.56.102:2181,192.168.56.103:2181");

        server.setDefaultCollection("myCore2");

        SolrInputDocument document = new SolrInputDocument();
        document.addField("id","21");
        document.addField("title","华为手机,在你不知道的地方偷工减料的手机");

        server.add(document);

        server.commit();
    }

    @Test
    public void test11() throws IOException, SolrServerException {
        CloudSolrServer server = new CloudSolrServer("192.168.56.101:2181,192.168.56.102:2181,192.168.56.103:2181");
        server.setDefaultCollection("myCore2");

        server.deleteById("20");
        server.commit();

    }

    @Test
    public void testquery() throws SolrServerException {
        CloudSolrServer server = new CloudSolrServer("192.168.56.101:2181,192.168.56.102:2181,192.168.56.103:2181");
        server.setDefaultCollection("myCore2");

        QueryResponse response = server.query(new SolrQuery("title:手机"));

        SolrDocumentList results = response.getResults();

        results.forEach(i ->{
            System.out.println(i.getFieldValue("id"));
            System.out.println(i.getFieldValue("title"));
        });
    }

}
